package com.yiren.manager.ctrl.cost;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrPaper;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.cost.YrPaperForm;
import com.yiren.manager.form.department.YrStaffInfoForm;
import com.yiren.manager.service.iface.cost.YrPaperService;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("paper")
@RestController
public class YrPaperController extends GenericController<YrPaperForm, YrPaper>{
	
	@Autowired
	private YrPaperService selfSevice;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	/**
	 * 保存票据编号
	 * @param data
	 * @return
	 * @throws MyException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws SwallowException
	 */
	@RequestMapping("save")
	@ResponseBody
	public HTTPResult<YrPaperForm> save(String data) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SwallowException{
		HTTPResult<YrPaperForm> result = new HTTPResult<YrPaperForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		
		YrPaperForm form = tool.mapper("paper", YrPaperForm.class);
		if(null==form){
			throwExp("param.error");
		}
		YrPaper entity = ObjectUtils.objectToObject(form, YrPaper.class);
		String sqlWhere = "staff_id=?1 AND paper_type=?2";
		QueryResult<YrPaper> qrPaper = selfSevice.find(YrPaper.class, sqlWhere, new Object[]{form.getStaffId(),form.getPaperType()});
		if(qrPaper.getResultList().isEmpty()){
			entity.setPaperId(UUIDGenerator.getUUID());
			entity.setCreateTime(new Date());	
		}else{
			YrPaper _paper = qrPaper.getResultList().get(0); 
			entity.setPaperId(_paper.getPaperId());
		}
		entity.setUseNum(form.getStartNum());
		entity.setUpdateTime(new Date());
		entity.setBatchNum(System.currentTimeMillis()+"-"+form.getStaffId());
		selfSevice.save(entity);
		
		return result;
	}
	
	/**
	 * 获取当前的票据号
	 * @param staffId 职工id 
	 * @param paperPype 票据类型（1发票2预交款收据3收费凭据）
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("getUserPaper")
	@ResponseBody
	public HTTPResult<YrPaperForm> getUserPaper(String staffId,Integer paperType) throws Exception{
		HTTPResult<YrPaperForm> result = new HTTPResult<YrPaperForm>();
		if(null==staffId){
			throwExp("param.error");
		}
		String sqlWhere;
		QueryResult<YrPaper> qrPaper ;
		if(null==paperType){
			sqlWhere = "staff_id=?1";
			qrPaper = selfSevice.find(YrPaper.class, sqlWhere, new Object[]{staffId});
		}else {
			sqlWhere = "staff_id=?1 AND paper_type=?2";
			qrPaper = selfSevice.find(YrPaper.class, sqlWhere, new Object[]{staffId,paperType});
		}
		
		List<YrPaperForm> formList = new ArrayList<>();
		
		for(YrPaper paper : qrPaper.getResultList()){
			YrPaperForm paperForm = toForm(paper);
			YrStaffInfo staffInfo = staffInfoService.findOne(paper.getStaffId());
			if(null!=staffInfo){
				paperForm.setStaffInfoForm(ObjectUtils.objectToObject(staffInfo, YrStaffInfoForm.class));
			}
			formList.add(paperForm);
		}
		result.setData(formList);
		
		return result;
	}
	
	@Override
	public YrPaperForm toForm(YrPaper e) throws Exception {
		YrPaperForm form = ObjectUtils.objectToObject(e, YrPaperForm.class);
		return form;
	}

	@Override
	public YrPaper toEntity(YrPaperForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	
}
